package com.exp3;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.table.DefaultTableModel;

public class ShowData extends Thread {
	
	private DefaultTableModel tm;
	private Map<String, PortGuidance> map;
	private boolean flag;
	
	Service service = new Service();
	public ShowData(DefaultTableModel tm, Map<String, PortGuidance> map, boolean flag) {
		this.tm = tm;
		this.map = map;
		this.flag = flag;
	}
	
	@Override
	public void run() {
		List<String> list = null;
		while (true) {
			try {
				Thread.sleep(2000l);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized (map) {
				if(!map.isEmpty()) {
					list = service.handleResultFromMapToList(map, flag);
				}else {
					try {
						map.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			Iterator<String> iterator = list.iterator();
			tm.setNumRows(0);
			int i = 0;
			while(iterator.hasNext()) {
				String next = iterator.next();
				tm.addRow(next.split("\\+"));
				if(++i %20 == 0) {
					try {
						Thread.sleep(2000l);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					tm.setNumRows(0);
				}
			}
		}
	}
}
